// pages/mall/index/index.js
const config = require('../../../utils/config.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    category: [],
    products: [],
    categoryId: '',
    more: '正在加载',
    isLastPage: false,
    pageNum: 1,
    pageSize: 10,
    isbg: '',
    imgUrls: [
      'https://m.360buyimg.com/babel/jfs/t1/6300/24/597/82291/5bc992efE76d8ea2f/ededb4b921e09515.jpg',
      'https://m.360buyimg.com/babel/jfs/t1/7484/5/130/100012/5bc92f45Eeb40e88a/b2e562f85002e126.jpg',
      'https://m.360buyimg.com/babel/jfs/t1/915/12/10406/83490/5bc92f79E88b2e8e3/a27a51315308b50b.jpg'
    ],
    steps: [{
        text: '',
        desc: '发布商品'
      },
      {
        text: '',
        desc: '送货到工作室'
      },
      {
        text: '',
        desc: '平台销售'
      },
      {
        text: '',
        desc: '坐等收钱'
      }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.startPullDownRefresh()
  },
  // 获取分类
  _getCategory() {
    let _this = this
    return new Promise((resolve, reject) => {
      wx.request({
        url: config.HOST + '/category/all',
        success(res) {
          // console.log(res.data.data.list)
          _this.setData({
            category: res.data.data.list
          })
          resolve(res.data.data.list)
        }
      })
    })
  },
  // 根据分类id获取商品
  _getProduct() {
    let _this = this
    wx.request({
      url: config.HOST + '/product/list',
      data: {
        categoryId: _this.data.categoryId,
        pageNum: this.data.pageNum++,
        pageSize: this.data.pageSize,
      },
      success(res) {
        console.log(res.data.data.list)
        if (res.data.data.isLastPage == true) {
          _this.data.more = '我也是有底线的'
        } else {
          _this.data.more = '正在加载'
        }
        let arr = _this.data.products.concat(res.data.data.list)
        _this.setData({
          products: arr,
          isLastPage: res.data.data.isLastPage,
          more: _this.data.more
        })

        wx.stopPullDownRefresh()
      }
    })
  },
  // 搜索栏下拉添加背景
  onPageScroll(data) {
    if (data.scrollTop > 100) {
      this.setData({
        isbg: 'bg'
      })
    } else {
      this.setData({
        isbg: ''
      })
    }
  },
  // 进入分类
  category() {
    wx.navigateTo({
      url: '../category/index',
    })
  },
  // 进入发布
  fubu() {
    wx.navigateTo({
      url: '../addProduct/index',
    })
  },
  // 切换分类
  changeCategory(e) {
    this.data.pageNum = 1
    this.setData({
      categoryId: e.currentTarget.dataset.id,
      products: []
    })
    this._getProduct()
  },
  // 进入搜索
  search() {
    wx.navigateTo({
      url: '../category/index?search=true',
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    let _this = this
    this.data.pageNum = 1
    this.setData({
      products: []
    })
    this._getCategory()
      .then((data) => {
        //获取到了分类
        console.log(data)
        _this.setData({
          categoryId: data[0].categoryId
        })
        _this._getProduct()
      })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (this.data.isLastPage) {
      return;
    }
    this._getProduct()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})